package address

import (
	"errors"
	"strings"

	"gitee.com/xgotool/backend-framework-template/config"
	"gitee.com/xiaoyutab/xgotool/xnum"
	"gitee.com/xiaoyutab/xgotool/xstring"
	"github.com/tealeg/xlsx"
)

// 省市区目录树结构
type City struct {
	Value    string `json:"value"`              // 值
	Label    string `json:"label"`              // 标签
	Children []City `json:"children,omitempty"` // 子选项
}

// 获取省市区下拉树结构
func CityTree() ([]City, error) {
	conf := config.GetExt()
	if conf.RunConf.CityExcel == "" {
		return nil, errors.New("后台暂未配置城市信息Excel文件路径！")
	}

	file, err := xlsx.OpenFile(conf.RunConf.CityExcel)
	if err != nil {
		return nil, err
	}
	if len(file.Sheets) == 0 {
		return nil, errors.New("表格内容为空")
	}
	sheet := file.Sheets[0] // 读取第一个工作表
	out := []City{}
	for i, row := range sheet.Rows {
		if i == 0 { // 跳过第一行
			continue
		}
		if len(row.Cells) < 3 { // 跳过不完整行
			continue
		}
		if row.Cells[0].String() == "中国" || row.Cells[0].String() == "" { // 跳过国家行
			continue
		}
		if strings.HasSuffix(row.Cells[1].String(), "0000") {
			// 省级
			out1 := City{
				Label:    row.Cells[0].String(),
				Value:    row.Cells[1].String(),
				Children: shiqu(row.Cells[1].String(), sheet),
			}
			out = append(out, out1)
		}
	}
	return out, nil
}

func shiqu(fid string, sheet *xlsx.Sheet) []City {
	if xnum.InArray(fid, []string{
		"",
		"710000", // 台湾省
	}) {
		// 特殊的几个省份直接返回空，无旗下的市辖区等信息
		return nil
	}
	out := []City{}
	if strings.HasSuffix(fid, "0000") && !xnum.InArray(fid, []string{
		"810000", // 香港
		"820000", // 澳门
	}) {
		// 省份，需要获取市级
		for i, row := range sheet.Rows {
			if i == 0 { // 跳过第一行
				continue
			}
			if len(row.Cells) < 3 { // 跳过不完整行
				continue
			}
			if xstring.ToInt(row.Cells[2].String()) == 0 { // 跳过非中国内地省份 非zero行
				continue
			}
			if strings.HasSuffix(row.Cells[1].String(), "00") && strings.HasPrefix(row.Cells[1].String(), fid[:2]) && !strings.HasSuffix(row.Cells[1].String(), "0000") {
				out1 := City{
					Label:    row.Cells[0].String(),
					Value:    row.Cells[1].String(),
					Children: shiqu(row.Cells[1].String(), sheet),
				}
				out = append(out, out1)
			}
		}
		if len(out) == 0 {
			// 该省为直辖市
			out = append(out, City{
				Label:    "市辖区",
				Value:    fid[:2] + "0100",
				Children: shiqu(fid[:2]+"0100", sheet),
			})
		}
		return out
	} else if strings.HasSuffix(fid, "00") {
		// 省份，需要获取市级
		for i, row := range sheet.Rows {
			if i == 0 { // 跳过第一行
				continue
			}
			if len(row.Cells) < 3 { // 跳过不完整行
				continue
			}
			if xstring.ToInt(row.Cells[2].String()) == 0 { // 跳过非中国内地省份 非zero行
				continue
			}
			if !strings.HasSuffix(row.Cells[1].String(), "00") && strings.HasPrefix(row.Cells[1].String(), fid[:4]) {
				out1 := City{
					Label:    row.Cells[0].String(),
					Value:    row.Cells[1].String(),
					Children: shiqu(row.Cells[1].String(), sheet),
				}
				out = append(out, out1)
			}
		}
		if xnum.InArray(fid, []string{
			"810000", // 香港
			"820000", // 澳门
		}) {
			out = []City{{
				Label:    "市辖区",
				Value:    fid,
				Children: out,
			}}
		}
		return out
	}
	return out
}
